package cn.zebra.dev.other.module.ordersDiscounts.service.impl;

import cn.zebra.dev.ibatis.plus.service.impl.ServiceImpl;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.ibatis.plus.utils.Query;
import cn.zebra.dev.other.module.ordersDiscounts.dao.DiscountsDao;
import cn.zebra.dev.other.module.ordersDiscounts.entity.DiscountsEntity;
import cn.zebra.dev.other.module.ordersDiscounts.service.DiscountsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 
 *
 * @author runnable
 */
@Service
public class DiscountsServiceImpl extends ServiceImpl<DiscountsDao, DiscountsEntity> implements DiscountsService {

    @Override
    public PageUtils<DiscountsEntity> findPage(Map<String, Object> params) {
        QueryWrapper<DiscountsEntity> wrapper = new QueryWrapper<>();
        wrapper.apply(params.containsKey("createId"),"create_id ={0}",params.get("createId"));
        IPage<DiscountsEntity> page = this.page(
                new Query<DiscountsEntity>(params).getPage(),
                wrapper
        );

        return new PageUtils<>(page);
    }

    @Override
    public DiscountsEntity findAllByCreateId(Long id) {
        QueryWrapper<DiscountsEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id, discounts_id, create_id, amount, discounts_type, create_time, expired_time, delete_flag");
        wrapper.apply("create_id = {0}" ,id);
        return this.getOne(wrapper);
    }

    @Override
    public boolean updateStatusById(Long id,int status) {
        DiscountsEntity discountsEntity = new DiscountsEntity();
        discountsEntity.setDiscountsStatus(status);
        return update(discountsEntity,"id = {0}",id);
    }

}
